package com.ywgt.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.Consumer;
import com.ywgt.system.service.ConsumerService;
import com.ywgt.system.vo.ConsumerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@RestController
@Api(value = "消费方模块",tags = "消费方模块")
@RequestMapping("/business/consumer")
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    @ApiOperation(value = "获取所有消费者",notes = "获取所有消费者")
    @GetMapping("/findAll")
    public Result findAll(){
        List<Consumer> consumers = consumerService.list();
        return Result.ok().data("rows",consumers);
    }
    @ApiOperation(value = "条件查询",notes = "根据条件查询")
    @GetMapping("/findConsumerList")
    public Result findConsumerList(@RequestParam(value = "pageNum",defaultValue = "1",required = false)Long pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "5",required = false)Long pageSize,
                                    Consumer consumer){
        Page<Consumer> consumerPage=consumerService.findConsumerList(pageNum,pageSize,consumer);
        List<Consumer> records = consumerPage.getRecords();
        long total = consumerPage.getTotal();
        return Result.ok().data("rows",records).data("total",total);
    }
    @ApiOperation(value = "获取信息",notes = "根据id获得信息")
    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable("id")Long id){
        Consumer consumer = consumerService.getById(id);
        return Result.ok().data("row",consumer);
    }
    @ApiOperation(value = "更新消费者信息",notes = "更新消费者信息")
    @PutMapping("/update/{id}")
    public Result update(@PathVariable("id")Long id,@RequestBody Consumer consumer){
        consumer.setId(id);
        if(consumerService.updateConsumer(consumer)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "删除信息",notes = "根据id删除消费者信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Long id){
        if (consumerService.removeById(id)){
           return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "添加消费者",notes = "添加消费者")
    @PostMapping("/add")
    public Result save(@RequestBody ConsumerVO consumerVO){
        if (consumerService.add(consumerVO)){
            return Result.ok();
        }
        return Result.error();
    }
}

